package com.situ.springboot.controller;

import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.BlogType;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.service.IBlogTypeService;
import com.situ.springboot.util.JSONResult;
import com.situ.springboot.util.LayUITableJSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//@Controller
//@ResponseBody
//public @interface RestController {

@Controller
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private IBlogService blogService;
    @Autowired
    private IBlogTypeService blogTypeService;

    @RequestMapping("/getBlogListPage")
    public String getBlogListPage() {
        return "blog_list";
    }

    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableJSONResult selectByPage(BlogQuery blogQuery) {
        System.out.println("BlogController.selectByPage");
        LayUITableJSONResult layUITableJSONResult = blogService.selectByPage(blogQuery);
        return layUITableJSONResult;
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.DELETE)
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        System.out.println("BlogController.deleteById");
        return blogService.deleteById(id);
    }

    @RequestMapping("/getBlogAddPage")
    public String getBlogAddPage(Model model) {
        List<BlogType> list = blogTypeService.selectAll();
        model.addAttribute("list", list);
        return "blog_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public JSONResult add(Blog blog) {
        return blogService.add(blog);
    }

    @RequestMapping("/getBlogUpdatePage")
    public String getBlogUpdatePage(Integer id, Model model) {
        Blog blog = blogService.selectById(id);
        model.addAttribute("blog", blog);
        List<BlogType> list = blogTypeService.selectAll();
        model.addAttribute("list", list);
        return "blog_update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(Blog blog) {
        return blogService.update(blog);
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(Integer[] ids) {
        return blogService.deleteAll(ids);
    }

    public void test() {
        System.out.println("BlogController.test");
        System.out.println("BlogController.test");
        System.out.println("BlogController.test");
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        System.out.println("BlogController.main");
        System.out.println("BlogController.main");
    }

}
